package com.rc.saas.mini.service.member;

import com.rc.saas.mini.common.base.TResponse;
import com.rc.saas.mini.mapper.member.MemberMultchargeApplyMapper;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.member.MemberMultchargeApply;
import com.rc.saas.mini.model.member.MemberMultchargeApplyExample;
import com.rc.saas.mini.model.order.OrderInfo;
import com.rc.saas.mini.service.order.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/4/25
 */
@Service
public class MemberMultchargeApplyServiceImpl implements MemberMultchargeApplyService {

    @Autowired
    private MemberMultchargeApplyMapper memberMultchargeApplyMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Override
    public MemberMultchargeApplyMapper getMapper() {
        return memberMultchargeApplyMapper;
    }

    @Override
    public MemberMultchargeApply selectByMemberId(Long memberId, String tenantCode) {
        MemberMultchargeApplyExample example = new MemberMultchargeApplyExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andMemberIdEqualTo(memberId);
        return memberMultchargeApplyMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public TResponse updateMemberMultcharge(String tenantCode, Long memberId, String chargeType, String applyType, String reason, MemberMultchargeApply memberMultchargeApply) {
        TResponse tResponse = TResponse.SUCCESS();
        if ("0".equals(applyType)) {
            if ("1".equals(chargeType)) {
                return TResponse.FAIL("当前会员已经是一号多充！");
            }
            //防止多次申请
            if (memberMultchargeApply == null) {
                MemberMultchargeApply multchargeApply = new MemberMultchargeApply();
                multchargeApply.setTenantCode(tenantCode);
                multchargeApply.setMemberId(memberId);
                multchargeApply.setApplyType("0");
                multchargeApply.setReason(reason);
                multchargeApply.setStatus("0");
                multchargeApply.setCreatedTime(new Date());
                multchargeApply.setLastUpdatedTime(new Date());
                multchargeApply.setCreatedBy(String.valueOf(memberId));
                memberMultchargeApplyMapper.insertSelective(multchargeApply);
                return tResponse;
            } else {
                MemberMultchargeApply multchargeApply = new MemberMultchargeApply();
                multchargeApply.setApplyId(memberMultchargeApply.getApplyId());
                multchargeApply.setApplyType("0");
                multchargeApply.setStatus("0");
                multchargeApply.setReason(reason);
                multchargeApply.setTenantCode(tenantCode);
                memberMultchargeApplyMapper.updateByPrimaryKeySelective(multchargeApply);
                tResponse.put(multchargeApply);
                return tResponse;
            }
        } else {
            //取消
            if ("0".equals(chargeType)) {
                return TResponse.FAIL("已经取消一号多充！");
            }

            MemberInfo memberInfo = new MemberInfo();
            memberInfo.setChargeType("0");
            memberInfo.setMemberId(memberId);
            memberInfo.setTenantCode(tenantCode);
            memberInfoService.updateByPrimaryKeySelective(memberInfo);

            MemberMultchargeApply multchargeApply = new MemberMultchargeApply();
            multchargeApply.setApplyId(memberMultchargeApply.getApplyId());
            multchargeApply.setApplyType("1");
            multchargeApply.setStatus("1");
            multchargeApply.setReason(reason);
            multchargeApply.setTenantCode(tenantCode);
            memberMultchargeApplyMapper.updateByPrimaryKeySelective(multchargeApply);
            tResponse.put(multchargeApply);
            return tResponse;
        }
    }
}
